****************************************************************************************************************************
Date: 17 FEB 2015

Purpose: Add child diagnosis

****************************************************************************************************************************;

libname in 'K:\Data\Workdata\702727\ADHD_grunddata\Datasæt';

*Benytter datasæt "alle_diag" som Anette har lavet. Det indeholder alle diagnoser fra LPR og PCR;

/* Fremgangsmåde ala Anette;
/*Tager to kopier af de forskellige diagnosevariable. 
Den "originale" variabel bruges til at vælge de relevante diagnoser udfra de to første bogstaver.
Den første kopi (k1) ændres til at have to cifre, hvorefter den bruges til at tilføje dummies for diagnoser, der kan karakteriseres 
ved to cifre.
Den anden kopi (k2) ændres til at have tre cifre, hvorefter den bruges til at tilføje dummies for diagnoser, der kan karakteriseres 
ved tre cifre;*/

data diag_kopi;
set in.alle_diag;


k1_adiag=adiag;
k1_diag=diag;
k1_psyk_adiag=psyk_adiag;
k1_hoveddiagnose=hoveddiagnose;
k1_grundmorbus=grundmorbus;
k1_bidiagnose_1=bidiagnose_1;
k1_bidiagnose_2=bidiagnose_2;
k1_bidiagnose_3=bidiagnose_3;
k1_bidiagnose_4=bidiagnose_4;
k1_bidiagnose_5=bidiagnose_5;
k1_bidiagnose_6=bidiagnose_6;
k1_bidiagnose_7=bidiagnose_7;
k1_bidiagnose_8=bidiagnose_8;
k1_bidiagnose_9=bidiagnose_9;
k1_bidiagnose_10=bidiagnose_10;
k1_bidiagnose_11=bidiagnose_11;
k1_bidiagnose_12=bidiagnose_12;
k1_bidiagnose_13=bidiagnose_13;
k1_bidiagnose_14=bidiagnose_14;

k2_adiag=adiag;
k2_diag=diag;
k2_psyk_adiag=psyk_adiag;
k2_hoveddiagnose=hoveddiagnose;
k2_grundmorbus=grundmorbus;
k2_bidiagnose_1=bidiagnose_1;
k2_bidiagnose_2=bidiagnose_2;
k2_bidiagnose_3=bidiagnose_3;
k2_bidiagnose_4=bidiagnose_4;
k2_bidiagnose_5=bidiagnose_5;
k2_bidiagnose_6=bidiagnose_6;
k2_bidiagnose_7=bidiagnose_7;
k2_bidiagnose_8=bidiagnose_8;
k2_bidiagnose_9=bidiagnose_9;
k2_bidiagnose_10=bidiagnose_10;
k2_bidiagnose_11=bidiagnose_11;
k2_bidiagnose_12=bidiagnose_12;
k2_bidiagnose_13=bidiagnose_13;
k2_bidiagnose_14=bidiagnose_14;

run;


data diag_kopi;

length adiag $ 2;
length diag $ 2;
length psyk_adiag $2;
length grundmorbus $2;
length bidiagnose_1 $2;
length bidiagnose_2 $2;
length bidiagnose_3 $2;
length bidiagnose_4 $2;
length bidiagnose_5 $2;
length bidiagnose_6 $2;
length bidiagnose_7 $2;
length bidiagnose_8 $2;
length bidiagnose_9 $2;
length bidiagnose_10 $2;
length bidiagnose_11 $2;
length bidiagnose_12 $2;
length bidiagnose_13 $2;
length bidiagnose_14 $2;


length k1_adiag$4;
length k1_diag $4;
length k1_psyk_adiag $4;
length k1_grundmorbus $4;
length k1_bidiagnose_1 $4;
length k1_bidiagnose_2 $4;
length k1_bidiagnose_3 $4;
length k1_bidiagnose_4 $4;
length k1_bidiagnose_5 $4;
length k1_bidiagnose_6 $4;
length k1_bidiagnose_7 $4;
length k1_bidiagnose_8 $4;
length k1_bidiagnose_9 $4;
length k1_bidiagnose_10 $4;
length k1_bidiagnose_11 $4;
length k1_bidiagnose_12 $4;
length k1_bidiagnose_13 $4;
length k1_bidiagnose_14 $4;

length k2_adiag$5;
length k2_diag $5;
length k2_psyk_adiag $5;
length k2_hoveddiagnose $5;
length k2_grundmorbus $5;
length k2_bidiagnose_1 $5;
length k2_bidiagnose_2 $5;
length k2_bidiagnose_3 $5;
length k2_bidiagnose_4 $5;
length k2_bidiagnose_5 $5;
length k2_bidiagnose_6 $5;
length k2_bidiagnose_7 $5;
length k2_bidiagnose_8 $5;
length k2_bidiagnose_9 $5;
length k2_bidiagnose_10 $5;
length k2_bidiagnose_11 $5;
length k2_bidiagnose_12 $5;
length k2_bidiagnose_13 $5;
length k2_bidiagnose_14 $5;

set diag_kopi;

run;


*Laver diagnoserne om til numeriske værdier;

data diag_kopi;
set diag_kopi;
k2_adiag=compress(k2_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_diag=compress(k2_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_hoveddiagnose=compress(k2_hoveddiagnose,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");

run;


data diag_kopi;
set diag_kopi;

n_adiag=input(k2_adiag,5.);
n_diag=input(k2_diag,5.);
n_hoveddiagnose=input(k2_hoveddiagnose,5.);

run;

********************************************************************************************************************
Koder først psykiske diagnoser fra før 1994 (ICD-8). Laver en indikator for om de har en psykisk diagnose (F=1)
*********************************************************************************************************************;
data diag_icd8;
set diag_kopi;

*Any psychiatric diagnosis;
if 29009<=n_adiag<=31599 then PSYCH_diag=1;
if 29009<=n_diag<=31599 then PSYCH_diag=1;
if 29009<=n_hoveddiagnose<=31599 then PSYCH_diag=1;

*Mental retardation;
if 31009<=n_adiag<=31599 then MR_diag=1;
if 31009<=n_diag<=31599 then MR_diag=1;
if 31009<=n_hoveddiagnose<=31599 then MR_diag=1;

*Behavioural and Emotional disorders;
if n_adiag=30809 then BE_diag=1;
if n_diag=30809 then BE_diag=1;
if n_hoveddiagnose=30809 then BE_diag=1;

*Cerebral Palsy and other Paralytic syndroms;
if 34309<=n_adiag<=34499 then CP_diag=1;
if 34309<=n_diag<=34499 then CP_diag=1;
if 34309<=n_hoveddiagnose<=34499 then CP_diag=1;

*Epilepsy;
if 34509<=n_adiag<=34599 then EP_diag=1;
if 34509<=n_diag<=34599 then EP_diag=1;
if 34509<=n_hoveddiagnose<=34599 then EP_diag=1;

*Visual problems;
if 36009<=n_adiag<=37999 then EYE_diag=1;
if 36009<=n_diag<=37999 then EYE_diag=1;
if 36009<=n_hoveddiagnose<=37999 then EYE_diag=1;

*Hearing impaired;
if 38009<=n_adiag<=38999 then EAR_diag=1;
if 38009<=n_diag<=38999 then EAR_diag=1;
if 38009<=n_hoveddiagnose<=38999 then EAR_diag=1;

*Asthma;
if n_adiag=49309 then ASTHMA=1;
if n_diag=49309 then ASTHMA=1;
if n_hoveddiagnose=49309 then ASTHMA=1;

*Congenital anomalies;
if 74009<=n_adiag<=75999 then CONGENITAL=1;
if 74009<=n_diag<=75999 then CONGENITAL=1;
if 74009<=n_hoveddiagnose<=75999 then CONGENITAL=1;
run;



*******************************************************
Kan nu gøre tilsvarende for ICD10 koderne

******************************************************;

*Fjerner bogstaverne i de kopierede variable;

data diag_icd10;
set diag_icd8;

k1_adiag=compress(k1_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_diag=compress(k1_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_psyk_adiag=compress(k1_psyk_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_grundmorbus=compress(k1_grundmorbus,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_1=compress(k1_bidiagnose_1,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_2=compress(k1_bidiagnose_2,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_3=compress(k1_bidiagnose_3,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_4=compress(k1_bidiagnose_4,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_5=compress(k1_bidiagnose_5,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_6=compress(k1_bidiagnose_6,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_7=compress(k1_bidiagnose_7,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_8=compress(k1_bidiagnose_8,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_9=compress(k1_bidiagnose_9,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_10=compress(k1_bidiagnose_10,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_11=compress(k1_bidiagnose_11,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_12=compress(k1_bidiagnose_12,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_13=compress(k1_bidiagnose_13,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_bidiagnose_14=compress(k1_bidiagnose_14,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");

k2_adiag=compress(k2_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_diag=compress(k2_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_psyk_adiag=compress(k2_psyk_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_grundmorbus=compress(k2_grundmorbus,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_1=compress(k2_bidiagnose_1,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_2=compress(k2_bidiagnose_2,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_3=compress(k2_bidiagnose_3,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_4=compress(k2_bidiagnose_4,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_5=compress(k2_bidiagnose_5,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_6=compress(k2_bidiagnose_6,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_7=compress(k2_bidiagnose_7,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_8=compress(k2_bidiagnose_8,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_9=compress(k2_bidiagnose_9,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_10=compress(k2_bidiagnose_10,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_11=compress(k2_bidiagnose_11,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_12=compress(k2_bidiagnose_12,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_13=compress(k2_bidiagnose_13,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_bidiagnose_14=compress(k2_bidiagnose_14,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");


run;

*Laver de kopierede variable om til numeriske værdier (n=numerisk);

data diag_icd10;
set diag_icd10;

nk1_adiag=input(k1_adiag,4.);
nk1_diag=input(k1_diag,4.);
nk1_grundmorbus=input(k1_grundmorbus,4.);
nk1_bidiagnose_1=input(k1_bidiagnose_1,4.);
nk1_bidiagnose_2=input(k1_bidiagnose_2,4.);
nk1_bidiagnose_3=input(k1_bidiagnose_3,4.);
nk1_bidiagnose_4=input(k1_bidiagnose_4,4.);
nk1_bidiagnose_5=input(k1_bidiagnose_5,4.);
nk1_bidiagnose_6=input(k1_bidiagnose_6,4.);
nk1_bidiagnose_7=input(k1_bidiagnose_7,4.);
nk1_bidiagnose_8=input(k1_bidiagnose_8,4.);
nk1_bidiagnose_9=input(k1_bidiagnose_9,4.);
nk1_bidiagnose_10=input(k1_bidiagnose_10,4.);
nk1_bidiagnose_11=input(k1_bidiagnose_11,4.);
nk1_bidiagnose_12=input(k1_bidiagnose_12,4.);
nk1_bidiagnose_13=input(k1_bidiagnose_13,4.);
nk1_bidiagnose_14=input(k1_bidiagnose_14,4.);

nk2_adiag=input(k2_adiag,5.);
nk2_diag=input(k2_diag,5.);
nk2_grundmorbus=input(k2_grundmorbus,5.);
nk2_bidiagnose_1=input(k2_bidiagnose_1,5.);
nk2_bidiagnose_2=input(k2_bidiagnose_2,5.);
nk2_bidiagnose_3=input(k2_bidiagnose_3,5.);
nk2_bidiagnose_4=input(k2_bidiagnose_4,5.);
nk2_bidiagnose_5=input(k2_bidiagnose_5,5.);
nk2_bidiagnose_6=input(k2_bidiagnose_6,5.);
nk2_bidiagnose_7=input(k2_bidiagnose_7,5.);
nk2_bidiagnose_8=input(k2_bidiagnose_8,5.);
nk2_bidiagnose_9=input(k2_bidiagnose_9,5.);
nk2_bidiagnose_10=input(k2_bidiagnose_10,5.);
nk2_bidiagnose_11=input(k2_bidiagnose_11,5.);
nk2_bidiagnose_12=input(k2_bidiagnose_12,5.);
nk2_bidiagnose_13=input(k2_bidiagnose_13,5.);
nk2_bidiagnose_14=input(k2_bidiagnose_14,5.);

run;
*Af en eller anden grund brokker SAS sig over, at jeg forsøger at ændre to nedenstående variable til numeriske værdier, 
men det ser ud til at lykkes alligevel;

data diag_icd10;
set diag_icd10;
nk2_psyk_adiag=input(k2_psyk_adiag,5.);
nk1_psyk_adiag=input(k1_psyk_adiag,4.);

run;


*dropper de kopierede diagnosevariable, der har en karakterværdi;

data diag_icd10_1;
set diag_icd10;

drop 
k1_adiag k1_diag k1_psyk_adiag k1_hoveddiagnose k1_grundmorbus k1_bidiagnose_1 k1_bidiagnose_2 k1_bidiagnose_3 k1_bidiagnose_4 k1_bidiagnose_5
k1_bidiagnose_6 k1_bidiagnose_7 k1_bidiagnose_8 k1_bidiagnose_9 k1_bidiagnose_10 k1_bidiagnose_11 k1_bidiagnose_12 k1_bidiagnose_13
k1_bidiagnose_14

k2_adiag k2_diag k2_psyk_adiag k2_hoveddiagnose k2_grundmorbus k2_bidiagnose_1 k2_bidiagnose_2 k2_bidiagnose_3 k2_bidiagnose_4 k2_bidiagnose_5
k2_bidiagnose_6 k2_bidiagnose_7 k2_bidiagnose_8 k2_bidiagnose_9 k2_bidiagnose_10 k2_bidiagnose_11 k2_bidiagnose_12 k2_bidiagnose_13
k2_bidiagnose_14;

run;


*Ændre rækkefølgen af variablene;

data diag_icd10_2;

retain pnr recnum sgh hospital diagtype adiag diag year_diag month_diag day_diag hoveddiagnose psyk_adiag year_psyk month_psyk day_psyk 
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

nk1_adiag nk1_diag nk1_psyk_adiag nk1_grundmorbus nk1_bidiagnose_1 nk1_bidiagnose_2 nk1_bidiagnose_3 nk1_bidiagnose_4 
nk1_bidiagnose_5 nk1_bidiagnose_6 nk1_bidiagnose_7 nk1_bidiagnose_8 nk1_bidiagnose_9 nk1_bidiagnose_10 nk1_bidiagnose_11
nk1_bidiagnose_12 nk1_bidiagnose_13 nk1_bidiagnose_14

nk2_adiag nk2_diag nk2_psyk_adiag nk2_grundmorbus nk2_bidiagnose_1 nk2_bidiagnose_2 nk2_bidiagnose_3 nk2_bidiagnose_4 
nk2_bidiagnose_5 nk2_bidiagnose_6 nk2_bidiagnose_7 nk2_bidiagnose_8 nk2_bidiagnose_9 nk2_bidiagnose_10 nk2_bidiagnose_11
nk2_bidiagnose_12 nk2_bidiagnose_13 nk2_bidiagnose_14

tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52  tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53

/*F90
F92
F*/
Psych_diag MR_diag BE_diag CP_diag EP_diag EYE_diag EAR_diag ASTHMA CONGENITAL;

set diag_icd10_1;
run;

proc freq data=diag_icd10_2;
table year_diag year_psyk ;
run;



Proc freq data=diag_icd10_2;
table PSYCH_diag MR_diag BE_diag CP_diag EP_diag EYE_diag EAR_diag ASTHMA CONGENITAL;
run;





* Tilføjer ICD10 diagnosticerede til dummies; 

data diag_icd10_3;
set diag_icd10_2;

*Any psychiatric diagnosis;
if adiag="DF" then PSYCH_diag=1;
if diag="DF" then PSYCH_diag=1;
if psyk_adiag="DF" then PSYCH_diag=1;
if grundmorbus="DF" then PSYCH_diag=1;
if bidiagnose_1="DF" then PSYCH_diag=1;
if bidiagnose_2="DF" then PSYCH_diag=1;
if bidiagnose_3="DF" then PSYCH_diag=1;
if bidiagnose_4="DF" then PSYCH_diag=1;
if bidiagnose_5="DF" then PSYCH_diag=1;
if bidiagnose_6="DF" then PSYCH_diag=1;
if bidiagnose_7="DF" then PSYCH_diag=1;
if bidiagnose_8="DF" then PSYCH_diag=1;
if bidiagnose_9="DF" then PSYCH_diag=1;
if bidiagnose_10="DF" then PSYCH_diag=1;
if bidiagnose_11="DF" then PSYCH_diag=1;
if bidiagnose_12="DF" then PSYCH_diag=1;
if bidiagnose_13="DF" then PSYCH_diag=1;
if bidiagnose_14="DF" then PSYCH_diag=1;


*Mental retardering;
if adiag="DF" &  70<=nk1_adiag <=79 then MR_diag=1;
if diag="DF" & 70<= nk1_diag <=79 then MR_diag=1;
if psyk_adiag="DF" & 70<= nk1_psyk_adiag <=79 then MR_diag=1;
if grundmorbus="DF" & 70<= nk1_grundmorbus <=79 then MR_diag=1;
if bidiagnose_1="DF" & 70<= nk1_bidiagnose_1 <=79 then MR_diag=1;
if bidiagnose_2="DF" & 70<= nk1_bidiagnose_2 <=79 then MR_diag=1;
if bidiagnose_3="DF" & 70<= nk1_bidiagnose_3 <=79 then MR_diag=1;
if bidiagnose_4="DF" & 70<= nk1_bidiagnose_4 <=79 then MR_diag=1;
if bidiagnose_5="DF" & 70<= nk1_bidiagnose_5 <=79 then MR_diag=1;
if bidiagnose_6="DF" & 70<= nk1_bidiagnose_6 <=79 then MR_diag=1;
if bidiagnose_7="DF" & 70<= nk1_bidiagnose_7 <=79 then MR_diag=1;
if bidiagnose_8="DF" & 70<= nk1_bidiagnose_8 <=79 then MR_diag=1;
if bidiagnose_9="DF" & 70<= nk1_bidiagnose_9 <=79 then MR_diag=1;
if bidiagnose_10="DF" & 70<= nk1_bidiagnose_10 <=79 then MR_diag=1;
if bidiagnose_11="DF" & 70<= nk1_bidiagnose_11 <=79 then MR_diag=1;
if bidiagnose_12="DF" & 70<= nk1_bidiagnose_12 <=79 then MR_diag=1;
if bidiagnose_13="DF" & 70<= nk1_bidiagnose_13 <=79 then MR_diag=1;
if bidiagnose_14="DF" & 70<= nk1_bidiagnose_14 <=79 then MR_diag=1;

*Udviklingsforstyrrelser af f.eks. sprog, motorik, dysleksi, indlæringsvanskeligheder;
if adiag="DF" &  80<=nk1_adiag <=83 then F80_83=1;
if diag="DF" & 80<= nk1_diag <=83 then F80_83=1;
if psyk_adiag="DF" & 80<= nk1_psyk_adiag <=83 then F80_83=1;
if grundmorbus="DF" & 80<= nk1_grundmorbus <=83 then F80_83=1;
if bidiagnose_1="DF" & 80<= nk1_bidiagnose_1 <=83 then F80_83=1;
if bidiagnose_2="DF" & 80<= nk1_bidiagnose_2 <=83 then F80_83=1;
if bidiagnose_3="DF" & 80<= nk1_bidiagnose_3 <=83 then F80_83=1;
if bidiagnose_4="DF" & 80<= nk1_bidiagnose_4 <=83 then F80_83=1;
if bidiagnose_5="DF" & 80<= nk1_bidiagnose_5 <=83 then F80_83=1;
if bidiagnose_6="DF" & 80<= nk1_bidiagnose_6 <=83 then F80_83=1;
if bidiagnose_7="DF" & 80<= nk1_bidiagnose_7 <=83 then F80_83=1;
if bidiagnose_8="DF" & 80<= nk1_bidiagnose_8 <=83 then F80_83=1;
if bidiagnose_9="DF" & 80<= nk1_bidiagnose_9 <=83 then F80_83=1;
if bidiagnose_10="DF" & 80<= nk1_bidiagnose_10 <=83 then F80_83=1;
if bidiagnose_11="DF" & 80<= nk1_bidiagnose_11 <=83 then F80_83=1;
if bidiagnose_12="DF" & 80<= nk1_bidiagnose_12 <=83 then F80_83=1;
if bidiagnose_13="DF" & 80<= nk1_bidiagnose_13 <=83 then F80_83=1;
if bidiagnose_14="DF" & 80<= nk1_bidiagnose_14 <=83 then F80_83=1;

*Behavioural and emotional disorders;
if adiag="DF" & 90<=nk1_adiag <=98 then BE_diag=1;
if diag="DF" & 90<= nk1_diag <=98 then BE_diag=1;
if psyk_adiag="DF" & 90<= nk1_psyk_adiag <=98 then BE_diag=1;
if grundmorbus="DF" & 90<= nk1_grundmorbus <=98 then BE_diag=1;
if bidiagnose_1="DF" & 90<= nk1_bidiagnose_1 <=98 then BE_diag=1;
if bidiagnose_2="DF" & 90<= nk1_bidiagnose_2 <=98 then BE_diag=1;
if bidiagnose_3="DF" & 90<= nk1_bidiagnose_3 <=98 then BE_diag=1;
if bidiagnose_4="DF" & 90<= nk1_bidiagnose_4 <=98 then BE_diag=1;
if bidiagnose_5="DF" & 90<= nk1_bidiagnose_5 <=98 then BE_diag=1;
if bidiagnose_6="DF" & 90<= nk1_bidiagnose_6 <=98 then BE_diag=1;
if bidiagnose_7="DF" & 90<= nk1_bidiagnose_7 <=98 then BE_diag=1;
if bidiagnose_8="DF" & 90<= nk1_bidiagnose_8 <=98 then BE_diag=1;
if bidiagnose_9="DF" & 90<= nk1_bidiagnose_9 <=98 then BE_diag=1;
if bidiagnose_10="DF" & 90<= nk1_bidiagnose_10 <=98 then BE_diag=1;
if bidiagnose_11="DF" & 90<= nk1_bidiagnose_11 <=98 then BE_diag=1;
if bidiagnose_12="DF" & 90<= nk1_bidiagnose_12 <=98 then BE_diag=1;
if bidiagnose_13="DF" & 90<= nk1_bidiagnose_13 <=98 then BE_diag=1;
if bidiagnose_14="DF" & 90<= nk1_bidiagnose_14 <=98 then BE_diag=1;

* ADHD;
if adiag="DF" & nk1_adiag=90 then ADHD=1;
if diag="DF" & nk1_diag=90 then ADHD=1;
if psyk_adiag="DF" & nk1_psyk_adiag=90 then ADHD=1;
if grundmorbus="DF" &  nk1_grundmorbus=90 then ADHD=1;
if bidiagnose_1="DF" &  nk1_bidiagnose_1=90 then ADHD=1;
if bidiagnose_2="DF" &  nk1_bidiagnose_2=90 then ADHD=1;
if bidiagnose_3="DF" &  nk1_bidiagnose_3=90 then ADHD=1;
if bidiagnose_4="DF" &  nk1_bidiagnose_4=90 then ADHD=1;
if bidiagnose_5="DF" &  nk1_bidiagnose_5=90 then ADHD=1;
if bidiagnose_6="DF" &  nk1_bidiagnose_6=90 then ADHD=1;
if bidiagnose_7="DF" &  nk1_bidiagnose_7=90 then ADHD=1;
if bidiagnose_8="DF" &  nk1_bidiagnose_8=90 then ADHD=1;
if bidiagnose_9="DF" &  nk1_bidiagnose_9=90 then ADHD=1;
if bidiagnose_10="DF" &  nk1_bidiagnose_10=90 then ADHD=1;
if bidiagnose_11="DF" &  nk1_bidiagnose_11=90 then ADHD=1;
if bidiagnose_12="DF" &  nk1_bidiagnose_12=90 then ADHD=1;
if bidiagnose_13="DF" &  nk1_bidiagnose_13=90 then ADHD=1;
if bidiagnose_14="DF" &  nk1_bidiagnose_14=90 then ADHD=1;

*Cerebral palsy and other paralytic syndroms;
if adiag="DG" & 80<=nk1_adiag <=83 then CP_diag=1;
if diag="DG" & 80<= nk1_diag <=83 then CP_diag=1;
if psyk_adiag="DG" & 80<= nk1_psyk_adiag <=83 then CP_diag=1;
if grundmorbus="DG" & 80<= nk1_grundmorbus <=83 then CP_diag=1;
if bidiagnose_1="DG" & 80<= nk1_bidiagnose_1 <=83 then CP_diag=1;
if bidiagnose_2="DG" & 80<= nk1_bidiagnose_2 <=83 then CP_diag=1;
if bidiagnose_3="DG" & 80<= nk1_bidiagnose_3 <=83 then CP_diag=1;
if bidiagnose_4="DG" & 80<= nk1_bidiagnose_4 <=83 then CP_diag=1;
if bidiagnose_5="DG" & 80<= nk1_bidiagnose_5 <=83 then CP_diag=1;
if bidiagnose_6="DG" & 80<= nk1_bidiagnose_6 <=83 then CP_diag=1;
if bidiagnose_7="DG" & 80<= nk1_bidiagnose_7 <=83 then CP_diag=1;
if bidiagnose_8="DG" & 80<= nk1_bidiagnose_8 <=83 then CP_diag=1;
if bidiagnose_9="DG" & 80<= nk1_bidiagnose_9 <=83 then CP_diag=1;
if bidiagnose_10="DG" & 80<= nk1_bidiagnose_10 <=83 then CP_diag=1;
if bidiagnose_11="DG" & 80<= nk1_bidiagnose_11 <=83 then CP_diag=1;
if bidiagnose_12="DG" & 80<= nk1_bidiagnose_12 <=83 then CP_diag=1;
if bidiagnose_13="DG" & 80<= nk1_bidiagnose_13 <=83 then CP_diag=1;
if bidiagnose_14="DG" & 80<= nk1_bidiagnose_14 <=83 then CP_diag=1;

*Epilepsy;
if adiag="DG" & 40<=nk1_adiag <=41 then EP_diag=1;
if diag="DG" & 40<= nk1_diag <=41 then EP_diag=1;
if psyk_adiag="DG" & 40<= nk1_psyk_adiag <=41 then EP_diag=1;
if grundmorbus="DG" & 40<= nk1_grundmorbus <=41 then EP_diag=1;
if bidiagnose_1="DG" & 40<= nk1_bidiagnose_1 <=41 then EP_diag=1;
if bidiagnose_2="DG" & 40<= nk1_bidiagnose_2 <=41 then EP_diag=1;
if bidiagnose_3="DG" & 40<= nk1_bidiagnose_3 <=41 then EP_diag=1;
if bidiagnose_4="DG" & 40<= nk1_bidiagnose_4 <=41 then EP_diag=1;
if bidiagnose_5="DG" & 40<= nk1_bidiagnose_5 <=41 then EP_diag=1;
if bidiagnose_6="DG" & 40<= nk1_bidiagnose_6 <=41 then EP_diag=1;
if bidiagnose_7="DG" & 40<= nk1_bidiagnose_7 <=41 then EP_diag=1;
if bidiagnose_8="DG" & 40<= nk1_bidiagnose_8 <=41 then EP_diag=1;
if bidiagnose_9="DG" & 40<= nk1_bidiagnose_9 <=41 then EP_diag=1;
if bidiagnose_10="DG" & 40<= nk1_bidiagnose_10 <=41 then EP_diag=1;
if bidiagnose_11="DG" & 40<= nk1_bidiagnose_11 <=41 then EP_diag=1;
if bidiagnose_12="DG" & 40<= nk1_bidiagnose_12 <=41 then EP_diag=1;
if bidiagnose_13="DG" & 40<= nk1_bidiagnose_13 <=41 then EP_diag=1;
if bidiagnose_14="DG" & 40<= nk1_bidiagnose_14 <=41 then EP_diag=1;

*DISEASES OF EYE;
if adiag="DH" & 00<=nk1_adiag <=59 then EYE_diag=1;
if diag="DH" & 00<= nk1_diag <=59 then EYE_diag=1;
if psyk_adiag="DH" & 00<= nk1_psyk_adiag <=59 then EYE_diag=1;
if grundmorbus="DH" & 00<= nk1_grundmorbus <=59 then EYE_diag=1;
if bidiagnose_1="DH" & 00<= nk1_bidiagnose_1 <=59 then EYE_diag=1;
if bidiagnose_2="DH" & 00<= nk1_bidiagnose_2 <=59 then EYE_diag=1;
if bidiagnose_3="DH" & 00<= nk1_bidiagnose_3 <=59 then EYE_diag=1;
if bidiagnose_4="DH" & 00<= nk1_bidiagnose_4 <=59 then EYE_diag=1;
if bidiagnose_5="DH" & 00<= nk1_bidiagnose_5 <=59 then EYE_diag=1;
if bidiagnose_6="DH" & 00<= nk1_bidiagnose_6 <=59 then EYE_diag=1;
if bidiagnose_7="DH" & 00<= nk1_bidiagnose_7 <=59 then EYE_diag=1;
if bidiagnose_8="DH" & 00<= nk1_bidiagnose_8 <=59 then EYE_diag=1;
if bidiagnose_9="DH" & 00<= nk1_bidiagnose_9 <=59 then EYE_diag=1;
if bidiagnose_10="DH" & 00<= nk1_bidiagnose_10 <=59 then EYE_diag=1;
if bidiagnose_11="DH" & 00<= nk1_bidiagnose_11 <=59 then EYE_diag=1;
if bidiagnose_12="DH" & 00<= nk1_bidiagnose_12 <=59 then EYE_diag=1;
if bidiagnose_13="DH" & 00<= nk1_bidiagnose_13 <=59 then EYE_diag=1;
if bidiagnose_14="DH" & 00<= nk1_bidiagnose_14 <=59 then EYE_diag=1;

*DISEASES OF EAR;
if adiag="DH" & 60<=nk1_adiag <=95 then EAR_diag=1;
if diag="DH" & 60<= nk1_diag <=95 then EAR_diag=1;
if psyk_adiag="DH" & 60<= nk1_psyk_adiag <=95 then EAR_diag=1;
if grundmorbus="DH" & 60<= nk1_grundmorbus <=95 then EAR_diag=1;
if bidiagnose_1="DH" & 60<= nk1_bidiagnose_1 <=95 then EAR_diag=1;
if bidiagnose_2="DH" & 60<= nk1_bidiagnose_2 <=95 then EAR_diag=1;
if bidiagnose_3="DH" & 60<= nk1_bidiagnose_3 <=95 then EAR_diag=1;
if bidiagnose_4="DH" & 60<= nk1_bidiagnose_4 <=95 then EAR_diag=1;
if bidiagnose_5="DH" & 60<= nk1_bidiagnose_5 <=95 then EAR_diag=1;
if bidiagnose_6="DH" & 60<= nk1_bidiagnose_6 <=95 then EAR_diag=1;
if bidiagnose_7="DH" & 60<= nk1_bidiagnose_7 <=95 then EAR_diag=1;
if bidiagnose_8="DH" & 60<= nk1_bidiagnose_8 <=95 then EAR_diag=1;
if bidiagnose_9="DH" & 60<= nk1_bidiagnose_9 <=95 then EAR_diag=1;
if bidiagnose_10="DH" & 60<= nk1_bidiagnose_10 <=95 then EAR_diag=1;
if bidiagnose_11="DH" & 60<= nk1_bidiagnose_11 <=95 then EAR_diag=1;
if bidiagnose_12="DH" & 60<= nk1_bidiagnose_12 <=95 then EAR_diag=1;
if bidiagnose_13="DH" & 60<= nk1_bidiagnose_13 <=95 then EAR_diag=1;
if bidiagnose_14="DH" & 60<= nk1_bidiagnose_14 <=95 then EAR_diag=1;


*Asthma;
if adiag="DJ" & nk1_adiag =45 then ASTHMA=1;
if diag="DJ" & nk1_diag =45 then ASTHMA=1;
if psyk_adiag="DJ" & nk1_psyk_adiag =45 then ASTHMA=1;
if grundmorbus="DJ" & nk1_grundmorbus =45 then ASTHMA=1;
if bidiagnose_1="DJ" & nk1_bidiagnose_1 =45 then ASTHMA=1;
if bidiagnose_2="DJ" & nk1_bidiagnose_2 =45 then ASTHMA=1;
if bidiagnose_3="DJ" & nk1_bidiagnose_3 =45 then ASTHMA=1;
if bidiagnose_4="DJ" & nk1_bidiagnose_4 =45 then ASTHMA=1;
if bidiagnose_5="DJ" & nk1_bidiagnose_5 =45 then ASTHMA=1;
if bidiagnose_6="DJ" & nk1_bidiagnose_6 =45 then ASTHMA=1;
if bidiagnose_7="DJ" & nk1_bidiagnose_7 =45 then ASTHMA=1;
if bidiagnose_8="DJ" & nk1_bidiagnose_8 =45 then ASTHMA=1;
if bidiagnose_9="DJ" & nk1_bidiagnose_9 =45 then ASTHMA=1;
if bidiagnose_10="DJ" & nk1_bidiagnose_10 =45 then ASTHMA=1;
if bidiagnose_11="DJ" & nk1_bidiagnose_11 =45 then ASTHMA=1;
if bidiagnose_12="DJ" & nk1_bidiagnose_12 =45 then ASTHMA=1;
if bidiagnose_13="DJ" & nk1_bidiagnose_13 =45 then ASTHMA=1;
if bidiagnose_14="DJ" & nk1_bidiagnose_14 =45 then ASTHMA=1;

run;


Proc freq data=diag_icd10_3;
table PSYCH_diag MR_diag BE_diag CP_diag EP_diag EYE_diag EAR_diag ASTHMA CONGENITAL F80_83 ADHD;
run;


*Udvælger diagnoser for at begrænse datasæt;

data diagnoser;
set diag_icd10_3;

if PSYCH_diag=1 or MR_diag=1 or BE_diag=1 or CP_diag=1 or EP_diag=1 or EYE_diag=1 or EAR_diag=1 or ASTHMA=1 or CONGENITAL=1
or F80_83=1 or ADHD=1
then output diagnoser;
run;


***********************************************************************************
Diagnose datoen er lige nu afhængig af om diagnosen er stillet i lpr er psyk reg.
	Laver nu en samlet diagnose dato
        
***********************************************************************************;
data diagnoser;
set diagnoser;

If PSYCH_diag=1 & dato_psyk="." 			then PSYCH_date=dato_diag;
If MR_diag=1 & dato_psyk="." 				then MR_date=dato_diag;
If BE_diag=1 & dato_psyk="." 				then BE_date=dato_diag;
If CP_diag=1 & dato_psyk="." 				then CP_date=dato_diag;
If EP_diag=1 & dato_psyk="." 				then EP_date=dato_diag;
If EYE_diag=1 & dato_psyk="." 				then EYE_date=dato_diag;
If EAR_diag=1 & dato_psyk="." 				then EAR_date=dato_diag;
If ASTHMA=1 & dato_psyk="." 				then ASTHMA_date=dato_diag;
if CONGENITAL=1 & dato_psyk="." 			then CONGENITAL_date=dato_diag; 
If F80_83=1 & dato_psyk="." 				then F80_83_date=dato_diag;
If ADHD=1 & dato_psyk="." 					then ADHD_date=dato_diag;

If PSYCH_diag=1 & dato_diag="." 			then PSYCH_date=dato_psyk;
If MR_diag=1 & dato_diag="." 				then MR_date=dato_psyk;
If BE_diag=1 & dato_diag="." 				then BE_date=dato_psyk;
If CP_diag=1 & dato_diag="." 				then CP_date=dato_psyk;
If EP_diag=1 & dato_diag="." 				then EP_date=dato_psyk;
If EYE_diag=1 & dato_diag="." 				then EYE_date=dato_psyk;
If EAR_diag=1 & dato_diag="." 				then EAR_date=dato_psyk;
If ASTHMA=1 & dato_diag="." 				then ASTHMA_date=dato_psyk;
if CONGENITAL=1 & dato_diag="." 			then CONGENITAL_date=dato_psyk; 
If F80_83=1 & dato_diag="." 				then F80_83_date=dato_psyk;
If ADHD=1 & dato_diag="." 					then ADHD_date=dato_psyk;
run;


proc sort data=diagnoser;
by pnr;
run;

*Jeg fjerner de variable jeg ikke skal bruge;

data diagnoser1;
set diagnoser;
drop 
recnum sgh hospital diagtype adiag diag  hoveddiagnose psyk_adiag
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

n_adiag n_diag n_hoveddiagnose 
nk1_adiag nk1_diag nk1_grundmorbus nk1_bidiagnose_1 nk1_bidiagnose_2 nk1_bidiagnose_3 nk1_bidiagnose_4 nk1_bidiagnose_5 nk1_bidiagnose_6 
nk1_bidiagnose_7 nk1_bidiagnose_8 nk1_bidiagnose_9 nk1_bidiagnose_10 nk1_bidiagnose_11 nk1_bidiagnose_12 nk1_bidiagnose_13 nk1_bidiagnose_14
nk1_psyk_adiag nk2_psyk_adiag dato_diag dato_psyk
nk2_adiag nk2_diag nk2_grundmorbus nk2_bidiagnose_1 nk2_bidiagnose_2 nk2_bidiagnose_3 nk2_bidiagnose_4 nk2_bidiagnose_5 nk2_bidiagnose_6 
nk2_bidiagnose_7 nk2_bidiagnose_8 nk2_bidiagnose_9 nk2_bidiagnose_10 nk2_bidiagnose_11 nk2_bidiagnose_12 nk2_bidiagnose_13 nk2_bidiagnose_14
tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52 tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53


year_diag month_diag day_diag year_psyk month_psyk day_psyk
;

run;


data diagnoser2;

retain 
pnr
Psych_diag PSYCH_date 
MR_diag MR_date 
BE_diag BE_date 
CP_diag CP_date
EP_diag EP_date
EYE_diag EYE_date 
EAR_diag EAR_date
ASTHMA ASTHMA_date 
CONGENITAL CONGENITAL_date 
F80_83 F80_83_date 
ADHD ADHD_date;

set diagnoser1;
run;

proc sort data=diagnoser2 out=diagnoser3 noduplicates;
by _all_;
run;

*STATA datasæt;
proc export data=diagnoser3 outfile="K:\Data\Workdata\702727\FBOI\Data\diagnosis.dta" dbms=stata replace;
run;
